package script.reciteWord
{
	import laya.components.Script;
	import laya.events.Event;
	import laya.utils.Handler;
	
	import model.HttpErrorCode;
	import model.HttpRequestUtil;
	import model.reciteWord.ReciteDateVo;
	
	import script.ViewManager;
	import script.reciteWord.items.ReciteDateCell;
	
	import ui.learn.ChooseReciteDatePanelUI;
	
	import utils.UtilTool;
	
	public class ChooseReciteDateController extends Script
	{
		private var uiSkin:ChooseReciteDatePanelUI;
		
		private var param:Object;
		
		private var curPage:int = 1;
		private var totalPage:int = 1;
		private var pageSize:int = 30;
		
		public function ChooseReciteDateController()
		{
			super();
		}
		override public function onStart():void
		{
			uiSkin = this.owner as ChooseReciteDatePanelUI;
			
			uiSkin.datelist.itemRender = ReciteDateCell;
			
			//uiSkin.orderList.vScrollBarSkin = "";
			uiSkin.datelist.repeatX = 2;
			uiSkin.datelist.spaceY = 5;
			
			uiSkin.datelist.renderHandler = new Handler(this, updateDatelist);
			uiSkin.datelist.selectEnable = false;
			uiSkin.datelist.array = [];
			
			uiSkin.curLevel.text = param.levelName;
			uiSkin.wordTest.on(Event.CLICK,this,function(){
				
				ViewManager.instance.openView(ViewManager.VIEW_DICTATION_PANEL,false,param);
			});
			
			
			uiSkin.startYear.labels = "2025,2024";
			uiSkin.endYear.labels = "2025,2024";

			uiSkin.startMonth.labels = "01,02,03,04,05,06,07,08,09,10,11,12";
			uiSkin.endMonth.labels = "01,02,03,04,05,06,07,08,09,10,11,12";
			
			var curdate:Date = new Date();
			var curMont:String = (curdate.getMonth()+1) >= 10?(curdate.getMonth()+1).toString():("0" + (curdate.getMonth()+1));
			
			uiSkin.startMonth.selectedLabel = curMont;
			
			uiSkin.startYear.selectedLabel = curdate.getFullYear() + "";
			if(curdate.getMonth()+1 >= 12)
			{
				uiSkin.endYear.selectedLabel = (curdate.getFullYear() + 1).toString();
				uiSkin.endMonth.selectedLabel =  "01";

			}
			else
			{
				uiSkin.endYear.selectedLabel = curdate.getFullYear() + "" ;
				uiSkin.endMonth.selectedLabel =  (curdate.getMonth()+2) >= 10?(curdate.getMonth()+2).toString():("0" + (curdate.getMonth()+2));
			}
		
			

			var postdata:String = "belongLevel=" + param.id + "&page=1&limit=" + pageSize + "&startTime=" +uiSkin.startYear.selectedLabel + "-" + uiSkin.startMonth.selectedLabel + "-01 00:00:00" + "&endTime=" + uiSkin.endYear.selectedLabel + "-" + uiSkin.endMonth.selectedLabel + "-01 00:00:00";
			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.getReciteDate,this,onGetDatesBack,postdata,"post");
			
		}
		
		
		private function getReciteDate():void
		{
			var postdata:String = "belongLevel=" + param.id + "&page=" + curPage + "&limit=" + pageSize + "&startTime=" + uiSkin.startYear.selectedLabel + "-" + uiSkin.startMonth.selectedLabel + "-01 00:00:00" + "&endTime=" + uiSkin.endYear.selectedLabel + "-" + uiSkin.endMonth.selectedLabel + "-01 00:00:00";
			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.getReciteDate,this,onGetDatesBack,postdata,"post");
		}
		
		private function onGetDatesBack(data:Object):void
		{
			if(this.destroyed)
				return;
			
			
			var result:Object = JSON.parse(data);
			if(result.code == HttpErrorCode.OK)
			{
				var arr:Array = [];
				var hastoday:Boolean = false;
				var curdate:String = UtilTool.formatFullDateTime(new Date(),false);
				for(var i:int=0;i < result.data.length;i++)
				{
					if(curPage == 1 && result.data[i].createTime.split(" ")[0] == curdate)
					{
						hastoday = true;
					}
					arr.push(new ReciteDateVo(result.data[i]));
				}
				
				if(curPage == 1)
				{							
					if(!hastoday)
					{
						arr.push(new ReciteDateVo({"id":-1,"createTime":(curdate+" 00:00:00"),"belongLevel":param.id,"wordList":"[]"}));
					}
				
				}
				
				uiSkin.datelist.array = arr;
			}
		}
		private function onPrevPage():void
		{
			if(curPage > 0)
			{
				curPage--;
				getReciteDate();
			}
		}
		
		private function onNextWord():void
		{
			if(curPage < totalPage)
			{
				curPage++;
				getReciteDate();
			}
		}
		
		public function updateDatelist(cell:ReciteDateCell):void
		{
			cell.setData(cell.dataSource);
		}
		
	}
}